
<%@page import="org.ow2.bonita.facade.uuid.ActivityInstanceUUID"%>
<%@page import="org.ow2.bonita.util.BusinessArchiveFactory"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ProcessDefinition"%>
<%@page import="org.ow2.bonita.facade.def.element.BusinessArchive"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.File"%>
<%@page import="org.ow2.bonita.facade.uuid.ProcessInstanceUUID"%>
<%@page import="org.ow2.bonita.facade.RuntimeAPI"%>
<%@page import="java.util.HashSet"%>
<%@page import="java.util.Set"%>
<%@ page import="javax.security.auth.login.LoginContext"%>

<%@ page import="org.ow2.bonita.facade.ManagementAPI"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>

<%
  final Object isAdminObject = request.getSession().getAttribute("isadmin");
  boolean isAdmin = false;
  if (isAdminObject != null && ((Boolean) isAdminObject).booleanValue()) {
    isAdmin = true;
  }
  final String back = request.getParameter("back");
  if (back == null) {
    response.sendRedirect("../home.jsp?msg=No return page was specified");
  }
  
  final String action = request.getParameter("action");

  if (action == null) {
    response.sendRedirect("../" + back + ".jsp?msg=Null action");
  }
  final Set<String> adminOnlyActions = new HashSet<String>();
  adminOnlyActions.add("archiveProcess");
  adminOnlyActions.add("cancelInstance");
  adminOnlyActions.add("deleteInstance");
  adminOnlyActions.add("deleteProcess");
  adminOnlyActions.add("deployProcess");
  adminOnlyActions.add("disableProcess");
  adminOnlyActions.add("enableProcess");

  if (!isAdmin && adminOnlyActions.contains(action)) {
    response.sendRedirect("../home.jsp");
  }

  String msg = null;
  String errorMsg = null;
  try {
    final String username = (String) request.getSession().getAttribute("username");
    final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
    loginContext.login();

    if ("archiveProcess".equals(action)) {
      
      final String processUUID = request.getParameter("processUUID");
      errorMsg = "Error while archiving process with uuid " + processUUID;
      final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
      managementAPI.archive(new ProcessDefinitionUUID(processUUID));
      msg = "Process " + processUUID + " successfully archived.";
      
    } else if ("cancelInstance".equals(action)) {
      
      final String instanceUUID = request.getParameter("instanceUUID");
      errorMsg = "Error while canceling instance with uuid " + instanceUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      //Cancel the process instance with the given instance UUID.
      runtimeAPI.cancelProcessInstance(new ProcessInstanceUUID(instanceUUID));
      msg = "Instance " + instanceUUID + " was successfully canceled.";
    
    } else if ("createInstance".equals(action)) {
      
      final String processUUID = request.getParameter("processUUID");
      errorMsg = "Error while creating a new instance of process with uuid " + processUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      final ProcessInstanceUUID instanceUUID = runtimeAPI.instantiateProcess(new ProcessDefinitionUUID(processUUID));
      msg = "A new instance of process " + processUUID + " was successfully created: " + instanceUUID;
    
    } else if ("deleteInstance".equals(action)) {
      
      final String instanceUUID = request.getParameter("instanceUUID");
      errorMsg = "Error while deleteing instance with uuid " + instanceUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      //Elimina tutti gli oggetti di runtime per l'istanza di processo con UUID 
      //ed eliminare anche i dati registrati in journal.
      runtimeAPI.deleteProcessInstance(new ProcessInstanceUUID(instanceUUID));
      msg = "Instance " + instanceUUID + " was successfully deleted.";
    
    } else if ("deleteProcess".equals(action)) {
      
      final String processUUID = request.getParameter("processUUID");
      errorMsg = "Error while deleting process with uuid " + processUUID;
      final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
      managementAPI.deleteProcess(new ProcessDefinitionUUID(processUUID));
      msg = "Process " + processUUID + " successfully deleted.";
    
    } else if ("deployProcess".equals(action)) {
      
      final String barFile = request.getParameter("barFile");
      errorMsg = "Error while deploying file " + barFile;
      final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
      final File file = new File(barFile);
      if (!file.exists()) {
        throw new FileNotFoundException(barFile);
      }
      final BusinessArchive businessArchive = BusinessArchiveFactory.getBusinessArchive(file);
      final ProcessDefinition process = managementAPI.deploy(businessArchive);
      msg = "Process " + process.getUUID() + " successfully deployed.";
    
    } else if ("disableProcess".equals(action)) {
      
      final String processUUID = request.getParameter("processUUID");
      errorMsg = "Error while disabeling process with uuid " + processUUID;
      final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
      managementAPI.disable(new ProcessDefinitionUUID(processUUID));
      msg = "Process " + processUUID + " successfully disabled.";
    
    } else if ("enableProcess".equals(action)) {
      
      final String processUUID = request.getParameter("processUUID");
      errorMsg = "Error while enabeling process with uuid " + processUUID;
      final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
      managementAPI.enable(new ProcessDefinitionUUID(processUUID));
      msg = "Process " + processUUID + " successfully enabled.";
    
    } else if ("executeTask".equals(action)) {
      
      final String taskUUID = request.getParameter("taskUUID");
      errorMsg = "Error while executing task with uuid " + taskUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      runtimeAPI.executeTask(new ActivityInstanceUUID(taskUUID), true);
      msg = "Task " + taskUUID + " was successfully executed.";
    
    } else if ("setActivityVariable".equals(action)) {
      
      final String taskUUID = request.getParameter("taskUUID");
      final String variableName = request.getParameter("variableName");
      final String variableValue = request.getParameter("variableValue");
      errorMsg = "Error while updating variable with name " + variableName + " and value " + variableValue + " on task " + taskUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      runtimeAPI.setActivityInstanceVariable(new ActivityInstanceUUID(taskUUID), variableName, variableValue);
      msg = "Variable " + variableName + " was successfully updated with value " + variableValue + " on task " + taskUUID + ".";
    
    } else if ("setProcessVariable".equals(action)) {
      
      final String instanceUUID = request.getParameter("instanceUUID");
      final String variableName = request.getParameter("variableName");
      final String variableValue = request.getParameter("variableValue");
      errorMsg = "Error while updating variable with name " + variableName + " and value " + variableValue + " on instance " + instanceUUID;
      final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
      runtimeAPI.setProcessInstanceVariable(new ProcessInstanceUUID(instanceUUID), variableName, variableValue);
      msg = "Variable " + variableName + " was successfully updated with value " + variableValue + " on instance " + instanceUUID + ".";
    
    } else {
      response.sendRedirect("../" + back + ".jsp?msg=Unknown action: " + action);
    }
    
    
    loginContext.logout();
  } catch (Exception e) {
    e.printStackTrace();
    Throwable t = e;
    while (t.getCause() != null) {
      t = t.getCause();
    }
    msg = errorMsg + ": " + t.getMessage();
  }
  response.sendRedirect("../" + back + ".jsp?msg=" + msg);
%>
